package dat255.calendarclock.model.event.interval;


import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.LinkedList;

/**
 *	
 *	Class used to describe when and how a event happens. 
 *	Divided into two main sections, the imutable list of 
 *	past events and a formula describing the future events.
 * @author Marcus Hultman
 *
 */
public abstract class Interval implements Serializable{

	/**
	 * needed to store in files with objectOutputStream;
	 */
	protected static final long serialVersionUID = -6565931592361621179L;
	
	/**
	 * Used in subclasses for events that has happened, and therefore will not be changed when future events is changed.
	 */
	private LinkedList<GregorianCalendar> pastEvents = new LinkedList<GregorianCalendar>();

	/**
	 * Get the date of the first time the Event occurred/occurs.
	 * @return
	 */
	public GregorianCalendar getFirst() {
		if (!pastEvents.isEmpty())
			return pastEvents.getFirst();
		return nextDateOfEvent();
	}
	
	
	public void setTimeOfEvent(int hour, int minute){
		GregorianCalendar first = this.getFirst();
		first.set(GregorianCalendar.HOUR_OF_DAY, hour);
		first.set(GregorianCalendar.MINUTE, minute);
	}
	
	/** 
	 * 
	 * @param currentDate
	 * @return the next date, null otherwise.
	 */
	public GregorianCalendar nextDateOfEvent(){
		return nextDateOfEvent(new GregorianCalendar());
	}
	
	/** 
	 * 
	 * @param currentDate
	 * @return the next date in relation to date, null otherwise.
	 */
	public abstract GregorianCalendar nextDateOfEvent(GregorianCalendar date) ;
	
	public abstract boolean equals(Interval interval);

	/**
	 * Adds an event that has happened in past events
	 * @param date that has happened
	 */
	public void setHasHappened(GregorianCalendar date) {
		pastEvents.add(date);		
	}

	
	
}
